package org.apereo.cas.support.saml.services;

import org.apereo.cas.authentication.principal.Principal;
import org.apereo.cas.authentication.principal.Service;
import org.apereo.cas.authentication.principal.ShibbolethCompatiblePersistentIdGenerator;
import org.apereo.cas.support.saml.services.idp.metadata.SamlRegisteredServiceServiceProviderMetadataFacade;
import org.apereo.cas.support.saml.services.idp.metadata.cache.SamlRegisteredServiceCachingMetadataResolver;
import org.apereo.cas.util.CollectionUtils;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.opensaml.saml.saml2.metadata.EntityDescriptor;
import org.springframework.context.ApplicationContext;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * This is {@link EduPersonTargetedIdAttributeReleasePolicy}.
 *
 * @author Misagh Moayyed
 * @since 5.2.0
 */
@Slf4j
@ToString(callSuper = true)
@Setter
@Getter
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode(callSuper = true)
public class EduPersonTargetedIdAttributeReleasePolicy extends BaseSamlRegisteredServiceAttributeReleasePolicy {
    /**
     * The attribute name generated by this policy.
     */
    public static final String ATTRIBUTE_NAME_EDU_PERSON_TARGETED_ID = "eduPersonTargetedID";

    private static final long serialVersionUID = -1283755507124862357L;

    @JsonProperty
    private String salt;

    @JsonProperty
    private String attribute;

    @Override
    protected Map<String, List<Object>> getAttributesForSamlRegisteredService(final Map<String, List<Object>> attributes,
                                                                        final SamlRegisteredService service,
                                                                        final ApplicationContext applicationContext,
                                                                        final SamlRegisteredServiceCachingMetadataResolver resolver,
                                                                        final SamlRegisteredServiceServiceProviderMetadataFacade facade,
                                                                        final EntityDescriptor entityDescriptor,
                                                                        final Principal principal,
                                                                        final Service selectedService) {
        val releaseAttributes = new HashMap<String, List<Object>>();
        val persistentIdGenerator = new ShibbolethCompatiblePersistentIdGenerator(this.salt);
        persistentIdGenerator.setAttribute(this.attribute);
        val principalId = persistentIdGenerator.determinePrincipalIdFromAttributes(principal.getId(), attributes);
        LOGGER.debug("Selected principal id [{}] to generate [{}] for service [{}]",
            principalId, ATTRIBUTE_NAME_EDU_PERSON_TARGETED_ID, selectedService);
        val result = persistentIdGenerator.generate(principalId, selectedService);
        releaseAttributes.put(ATTRIBUTE_NAME_EDU_PERSON_TARGETED_ID, CollectionUtils.wrapList(result));
        LOGGER.debug("Calculated [{}] attribute as [{}]", ATTRIBUTE_NAME_EDU_PERSON_TARGETED_ID, result);
        return releaseAttributes;
    }
}
